package com.teach.javafxclient.controller.base;

import com.teach.javafxclient.MainApplication;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Liang Jianlin
 * @date 2024-04-03 00:13
 */
public class TabPaneController {
    private Map<String,Tab> tabMap = new HashMap<String,Tab>();
    private Map<Scene,Tab> tabSceneMap = new HashMap<>();
    private Map<String,Scene> sceneMap = new HashMap<String,Scene>();
    private Map<Scene,Scene> sceneSceneMap = new HashMap<>();
    private Map<String,ToolController> controlMap =new HashMap<String,ToolController>();
    private Map<Scene,ToolController> controlSceneMap =new HashMap<>();

    @FXML
    protected TabPane contentTabPane;
    public void changeContent(String name, String title) {
        Tab tab = tabMap.get(name);
        Scene scene;
        Object c;
        if(tab == null) {
            scene = sceneMap.get(name);
            if(scene == null) {
                FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource(name + ".fxml"));
                try {
                    scene = new Scene(fxmlLoader.load());
                    sceneMap.put(name, scene);
                } catch (IOException e) {
                    e.printStackTrace();
                    return;
                }
                c = fxmlLoader.getController();
                if(c instanceof ToolController) {
                    controlMap.put(name,(ToolController)c);
                }
            }
            tab = new Tab(title);
            tab.setId(name);
            tab.setOnSelectionChanged(this::tabSelectedChanged);
            tab.setOnClosed(this::tabOnClosed);
            tab.setContent(scene.getRoot());
            contentTabPane.getTabs().add(tab);
            tabMap.put(name, tab);
        }
        contentTabPane.getSelectionModel().select(tab);
    }
    public void changeContentByScene(Scene s,String name,String title) {
        Tab tab = tabSceneMap.get(s);
        Scene scene;
        Object c;
        if(tab == null) {
            scene = sceneSceneMap.get(s);
            if(scene == null) {
                scene = s;
                sceneSceneMap.put(s, scene);
            }
            tab = new Tab(title);
            tab.setId(name);
            tab.setOnSelectionChanged(this::tabSelectedChanged);
            tab.setOnClosed(this::tabOnClosed);
            tab.setContent(scene.getRoot());
            contentTabPane.getTabs().add(tab);
            tabSceneMap.put(s, tab);
        }
        contentTabPane.getSelectionModel().select(tab);
    }
    public void tabSelectedChanged(Event e) {
        Tab tab = (Tab)e.getSource();
//        System.out.println("Selection changed event for " + tab.getText() +
//                " tab, selected = " + tab.isSelected());
        Node node =tab.getContent();
        Scene scene = node.getScene();
    }
    public void tabOnClosed(Event e) {
        Tab tab = (Tab)e.getSource();
        String name = tab.getId();
        contentTabPane.getTabs().remove(tab);
        tabMap.remove(name);
    }
}
